const app = getApp();
const util = require('../../utils/util');
Page({

  data: {
    orderList: [],
    sumPrice: 0,
    numTotal: 0,
    addressNeed: true,
    note: '',
    orderNewid: 0,
    openid: '',
    goodsIdList: []
  },
  onLoad: function (options) {
    console.log(app.globalData.orderList);
    this.setData({
      orderList: app.globalData.orderList,
      sumPrice: app.globalData.sumPrice,
      numTotal: app.globalData.numTotal
    })
    this.getGoodsIdList();
  },
  // 获取商品的goodsid以便于后面的商品数量减少
  getGoodsIdList() {
    const len = app.globalData.orderList.length;
    console.log(len);
    for (let j in app.globalData.orderList) {
      this.data.goodsIdList.push(app.globalData.orderList[j].goodsid)
      console.log(this.data.goodsIdList);
    }
  },
  addAddress() {
    var that = this;
    console.log(111);
    wx.chooseAddress({
      success: (result) => {
        console.log(result);
        that.setData({
          phone: result.telNumber,
          buyname: result.userName,
          address: result.provinceName + result.cityName + result.countyName + result.detailInfo,
          addressNeed: false
        })
      },
    })
  },
  getNote(event) {
    // console.log(event.detail.value);
    this.setData({
      note: event.detail.value
    })

  },
  addOrder() {
    var that = this;
    if (this.data.addressNeed == true) {
      wx.showToast({
        title: '请先选择收货地址！',
        icon:'none'
      })
    } else {
      wx.request({
        url: `${app.globalData.baseurl}order/`,
        method: "POST",
        data: {
          openid: app.globalData.openid,
          note: this.data.note,
          orderList: this.data.orderList,
          buyname: this.data.buyname,
          address: this.data.address,
          phone: this.data.phone,
          time: util.formatTime(new Date),
          totalMoney: this.data.sumPrice,
          status: -1,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
        },
        success: res => {
          console.log(res);
          console.log(res.data);
          // 不成功orderNewid
          let orderId = res.data.insertId;
          // console.log(orderId);
          that.setData({
            orderNewid: res.data.insertId
          })
          that.xuniPay();
        },
      })
      // 调起虚拟支付
    }


  },
  xuniPay() {
    let that = this;
    console.log(this.data.orderNewid)
    wx.showModal({
      title: '提示',
      content: '是否支付商品合计' + this.data.sumPrice + '元',
      confirmText: '支付'
    }).then(res => {
      console.log(res);
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/changeStatus`,
          method: "POST",
          data: {
            orderNewid: this.data.orderNewid,
            status: 0,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            // 从购物车里面清除订单里面的商品
            that.clearCartList();
            console.log(res.data);
            wx.navigateBack({
              delta: 0,
              success() {
                wx.showToast({
                  title: '支付成功',
                })
              }
            })

          }
        })
      } else {
        wx.navigateBack({
          delta: 0,
          success() {
            wx.showToast({
              icon: 'error',
              title: '支付取消',
            })
          }
        })
      }

    })
  },
  // 清除购物车数据
  clearCartList() {
    console.log('cartList和orderList')
    console.log(app.globalData.cartList);
    console.log(app.globalData.orderList);
    for (let i in app.globalData.cartList) {
      for (let j in app.globalData.orderList) {
        if (app.globalData.orderList[j].goodsid == app.globalData.cartList[i].goodsid) {
          app.globalData.cartList.splice(i, 1);
        }
      }
    }
    wx.setStorageSync('cartList', app.globalData.cartList)
  }
  // 商品数量减少

})